package com.leonzx.base.service.system;

import com.leonzx.base.dao.system.RoleDAO;
import com.leonzx.base.dao.system.UserDAO;
import com.leonzx.base.dao.system.UserRoleDAO;
import com.leonzx.base.entity.system.Role;
import com.leonzx.base.service.BaseService;
import com.leonzx.base.utils.Constants;
import com.leonzx.base.utils.SQLUtils;
import com.leonzx.base.utils.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-08 15:13
 */
@Service
public class RoleService extends BaseService {
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private UserRoleDAO userRoleDAO;
    @Autowired
    private UserDAO userDAO;

    /**
     * 查询所有角色，搜索条件只有一个角色名称，like模糊查询
     *
     * @param role
     * @return
     */
    public List<Role> getAllBaseRole(Role role) {
        //有userId的时候为分配临时权限,查询我的所有权限

        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();        //如果UserID有值，查询临时权限
        if (StringUtils.isNotBlank(role.getName())) {
            criteria.andLike("name", SQLUtils.like(role.getName()));
        }if (!Objects.isNull(role.getState())) {
            criteria.andEqualTo("state", role.getState());
        }
        List<Role> roles = roleDAO.selectByExample(example);

        return roles;
    }

    /**
     * 有id 更新 無Id  插入
     *
     * @param baseRole
     */
    public void saveOrUpdate(Role baseRole) {
        if (StringUtils.isNotBlank(baseRole.getId())) {
            roleDAO.updateByPrimaryKeySelective(baseRole);
        } else {
            baseRole.setId(UUIDUtils.getUUID());
            baseRole.setCreateTime(new Date());
            baseRole.setState(1);
            roleDAO.insert(baseRole);
        }
    }

    public Role findById(Role role) {
        return roleDAO.selectByPrimaryKey(role);
    }

    public List<Role> getMyRolesOutHis(String myId, String hisId,String name) {

        return roleDAO.getMyRolesOutHis(myId, hisId,name);
    }


}
